package rmi;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;


public class bookManageImpl implements BookManage {

    private List<Book> books;

    public bookManageImpl(){
        this.books = new ArrayList<>();
    }

    private boolean exists(Book e){
        for(int i=0; i<books.size(); i++){
            if(books.get(i).getID() == e.getID()) return true;
        }
        return false;
    }
    @Override
    public int add(int a, int b) throws RemoteException{
        return a + b;
    }

    @Override
    public boolean add(Book b) throws RemoteException {
        if(!exists(b)) {
            return books.add(b);
        }
        return false;
    }

    @Override
    public Book queryById(int bookID) throws RemoteException {
        for(int i=0; i<books.size(); i++){
            if(books.get(i).getID() ==bookID) return books.get(i);
        }
        return null;
    }

    @Override
    public BookList queryByName(String name) throws RemoteException {
        BookList booklist = new BookList();
        for(int i=0; i<books.size(); i++){
            if(books.get(i).getName().equals(name)) {
                booklist.put(books.get(i));
            }
        }
        return booklist;
    }

    @Override
    public boolean delete(int bookID) throws RemoteException {
        boolean status = false;
        for(int i=0; i<books.size(); i++){
            if(books.get(i).getID() ==bookID) {
                books.remove(i);
                status = true;
            }
        }
        return status;
    }
}
